package cn.tedu.psychology.controller;

import cn.tedu.psychology.pojo.entity.User;
import cn.tedu.psychology.web.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Slf4j
@RequestMapping("/user")
@RestController
public class UserController {
    @PostMapping("/login")

        public JsonResult tologin(@RequestParam("username") String username, @RequestParam("password") String password, HttpSession session) {
            User user = new User();
            user.setUsername(username);
            user.setPassword(password);
            if (username.equals("张三") && password.equals("123")) {
                session.setAttribute("user", username);
            } else if (username.equals("李四") && password.equals("123")) {
                session.setAttribute("user", username);
            } else if (username.equals("123") && password.equals("123")) {
                session.setAttribute("user", username);
            } else if (username.equals("王五") && password.equals("123")) {
                session.setAttribute("user", username);
            }
        return JsonResult.ok(user);
    }

    @GetMapping("/getUsername")
    public String getUsername(HttpSession session) {
        String username = (String) session.getAttribute("user");
        log.info("username：{}", username);
        return username;
    }
}
